<?php
$params = array_merge(
    require __DIR__ . '/../../common/config/params.php',
    require __DIR__ . '/../../common/config/params-local.php',
    require __DIR__ . '/params.php',
    require __DIR__ . '/params-local.php'
);
$rules = array_merge(
    require(__DIR__ . '/rules.php'),
    require(__DIR__ . '/../../common/config/rules.php')
);
return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-api',
        ],
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if (($response->data !== null && Yii::$app->controller->module->id !== 'debug' && Yii::$app->controller->module->id !== 'gii' && Yii::$app->controller->id !== 'doc') || $response->statusCode != 200) {
                    $data = $response->data;
                    $code = isset($data['code']) ? $data['code'] : $response->statusCode;
                    $message = \common\helpers\CodeHelper::getCodeText($code);
                    if (isset($data['message']) && !empty($data['message'])) {
                        $message = \common\helpers\BaseHelper::isJson($data['message']);
                    }
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'code' => $code,
                        'message' => $message,
                        'data' => $data,
                    ];
                    $response->statusCode = 200;
                    $response->format = \yii\web\Response::FORMAT_JSON;

                }
            }
        ],
        'user' => [
            'identityClass' => 'server\dao\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
        ],
        /*'session' => [
            // this is the name of the session cookie used for login on the backend
            'name' => 'advanced-backend',
        ],*/
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,
            'rules' => $rules,
        ]
    ],
    'modules' => [
        'v1' => [
            'class' => 'api\modules\v1\Module',
        ],
    ],
    'params' => $params,

];
